package com.txy.domain.strategy.service.rule.chain.factory;

import com.txy.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import com.txy.domain.strategy.repository.IStrategyRepository;
import com.txy.domain.strategy.service.rule.chain.ILogicChain;
import com.txy.types.common.Constants;
import lombok.*;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class LogicChainFactory {

    private Map<String, ILogicChain> map;

    private IStrategyRepository repository;


    LogicChainFactory(Map<String, ILogicChain> map, IStrategyRepository repository) {
        this.map = map;
        this.repository = repository;
    }


    public ILogicChain openLogicChain(Long strategyId) {
        String ruleModels = repository.queryRuleModelByStrategyId(strategyId);

        if (ruleModels == null) {
            return map.get("default");
        }

        String[] split = ruleModels.split(Constants.SPLIT);
        ILogicChain logicChain = map.get(split[0]);
        ILogicChain current = logicChain;

        for (int i = 1; i < split.length; i++) {
            String ruleModel = split[i];
            ILogicChain iLogicChain = map.get(ruleModel);
            if (iLogicChain != null) {
                current = current.chainNext(iLogicChain);
            }
        }

        current.chainNext(map.get("default"));
        return logicChain;
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class LogicChainRes {
        private RuleLogicCheckTypeVO ruleLogicCheckTypeVO;
        private LogicChainAward logicChainAward;
        private LogicModel logicModel;

        public Integer getAwardId() {

            return logicChainAward.getAwardId();
        }
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class LogicChainAward {
        private Integer awardId;
    }

    @Getter
    @AllArgsConstructor
    public enum LogicModel {

        RULE_WIGHT("rule_weight","根据抽奖权重返回可抽奖范围KEY"),
        RULE_BLACKLIST("rule_blacklist","黑名单规则过滤，命中黑名单则直接返回"),
        DEFAULT("default","默认规则"),
        ;

        private final String code;
        private final String info;

    }
}
